package io.scnb.lesson001.demo7;

import org.springframework.beans.factory.DisposableBean;

/**
 * 强依赖的bean创建和销毁顺序
 * 依赖顺序：bean3->bean2->bean1（3依赖2，2依赖1）
 * bean对象的创建顺序和bean依赖的顺序一致(创建顺序1，2，3，即必须得先创建1，才能创建2，才能创建3)
 * bean对象销毁的顺序和bean对象创建的顺序相反
 */
public class StrongDependenceBean {
    public static class Bean1 implements DisposableBean {
        public Bean1() {
            System.out.println(this.getClass() + " constructor!");
        }

        @Override
        public void destroy() throws Exception {
            System.out.println(this.getClass() + " destroy()");
        }
    }

    public static class Bean2 implements DisposableBean {

        private Bean1 bean1;

        public Bean2(Bean1 bean1) {
            this.bean1 = bean1;
            System.out.println(this.getClass() + " constructor!");
        }

        @Override
        public void destroy() throws Exception {
            System.out.println(this.getClass() + " destroy()");
        }
    }

    public static class Bean3 implements DisposableBean {
        private Bean2 bean2;
        public Bean3(Bean2 bean2) {
            this.bean2 = bean2;
            System.out.println(this.getClass() + " constructor!");
        }


        @Override
        public void destroy() throws Exception {
            System.out.println(this.getClass() + " destroy()");
        }
    }
}
